﻿using UnityEngine;
using System.Collections;

public enum RollerDirection{Horizontal, Vertical}

public class RollerTrapController : MonoBehaviour {
	public RollerDirection rollerDirection;
	public Transform leftRail, rightRail;
	public Transform roller;
	public float speed;
	private float _rollerLength;
	private float _smallBound, _bigBound;
	void Awake(){
		if(rollerDirection == RollerDirection.Horizontal){
			_smallBound = Mathf.Min(leftRail.position.x, rightRail.position.x);
			_bigBound = Mathf.Max(leftRail.position.x, rightRail.position.x);
		}else{
			_smallBound = Mathf.Min(leftRail.position.y, rightRail.position.y);
			_bigBound = Mathf.Max(leftRail.position.y, rightRail.position.y);
		}
		_rollerLength = _bigBound - _smallBound;

	}

	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(rollerDirection == RollerDirection.Horizontal){
			roller.position = new Vector3(_smallBound + Mathf.PingPong(Time.time * speed, _rollerLength), roller.position.y, roller.position.z);
		}else{
			roller.position = new Vector3(roller.position.x, _smallBound + Mathf.PingPong(Time.time * speed, _rollerLength), roller.position.z);
		}
	//	roller.position = new Vector3( Mathf.PingPong(Time.time * speed, 
	}
}
